package com.cuitrip.business.notice;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.cuitrip.app.base.BaseItemDecoration;
import com.cuitrip.app.base.BaseVerticalListActivity;
import com.cuitrip.business.login.LoginInstance;
import com.cuitrip.business.notice.ui.IMessageListView;
import com.cuitrip.business.notice.ui.MessageAdapter;
import com.cuitrip.business.notice.ui.MessageMode;
import com.cuitrip.business.notice.ui.MessagePresent;
import com.cuitrip.business.user.model.CtUserInfo;
import com.cuitrip.component.statelayout.StateLayout;
import com.cuitrip.service.R;
import com.cuitrip.util.track.TrackUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.utils.LogHelper;
import com.lab.utils.Utils;

import java.util.List;

import butterknife.Bind;
import jp.wasabeef.recyclerview.animators.BaseItemAnimator;
import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;
import jp.wasabeef.recyclerview.animators.SlideInUpAnimator;

/**
 * Created by baziii on 15/8/10.
 */
public class MessageListActivity extends BaseVerticalListActivity<MessageMode> implements IMessageListView {
    public static final String TAG = "MessageListActivity";
    MessagePresent mMessagePresent = new MessagePresent(this);

    @Bind(R.id.state_layout)
    StateLayout stateLayout;

    protected View.OnClickListener mOnClickListener = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.ct_message_layout:
                    if (v.getTag()!=null) {
                        TrackUtil.trackScreenName("/user/inbox/message");
                        int position = (int) v.getTag();
                        MessageMode messageMode = mAdapter.getItem(position);
                        if (messageMode != null){
                            mMessagePresent.onClickMessage(messageMode);
                            messageMode.setIsRead(true);
                            mAdapter.notifyItemChanged(position);
                        }
                    }
                    break;
                case R.id.ct_delete:
                    if (v.getTag()!=null &&v.getTag() instanceof MessageMode) {
                        MessageMode messageMode = (MessageMode) v.getTag();
                        mMessagePresent.onMove(messageMode);
                    }
                    break;
                default:
                    break;
            }
        }
    };

    MessageAdapter mAdapter = new MessageAdapter(mOnClickListener);
    public static void startActivityForResult(Activity activity) {
        if (activity != null) {
            activity.startActivityForResult(new Intent(activity, MessageListActivity.class),2);
        }
    }

    @Override
    public void initView() {
        super.initView();
        stateLayout.setEmptyTitleRes(R.string.message_feedback_no_result);
        mRecyclerView.setAdapter(mAdapter);
        BaseItemAnimator animator = new SlideInUpAnimator();
        mRecyclerView.setItemAnimator(animator);
        mRecyclerView.addItemDecoration(new BaseItemDecoration(0,0,Utils.dp2pixel(1),0));
    }

    @Override
    public void initData() {
        super.initData();
        requestPresentRefresh();
    }

    @Override
    public void jumpMessage(MessageMode messageMode) {
        LogHelper.e(TAG,"jumpMessage :"+messageMode.toString());
//        BrowserActivity.startWithData(this,messageMode.content);
        MessageDetailActivity.start(this, messageMode);
    }

    @Override
    public void deleteMessage(MessageMode messageMode) {
            mAdapter.removeMessageMode(messageMode);
//        mAdapter.notifyDataSetChanged();
    }

    @Override
    public int getSize() {
        return mAdapter.getItemCount();
    }

    @Override
    public void renderUIWithData(List<MessageMode> items) {
        mAdapter.setModeList(items);
        updateStateLayout();
//        mAdapter.notifyDataSetChanged();
    }

    private void updateStateLayout() {
        if (mAdapter.getItemCount() == 0){
            stateLayout.changeState(StateLayout.State.Empty);
        }else{
            stateLayout.changeState(StateLayout.State.INIT);
        }
    }

    @Override
    public void renderUIWithAppendData(List<MessageMode> items) {
        mAdapter.appendModeList(items);
        updateStateLayout();
//        mAdapter.notifyDataSetChanged();

    }

    @Override
    public void requestPresentLoadMore() {
        uiShowLoadMore();
        mMessagePresent.requestLoadMore();
    }

    @Override
    public void requestPresentRefresh() {
        uiHideLoadMore();
        mMessagePresent.requestRefresh();

    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public void onTopbarLeftPress() {
        finish();
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config =new CustomUiConfig();
        config.mTopbarMiddleAreaName=getString(R.string.message);
        config.mTopbarLeftAreaName=getString(R.string.back_icon);
        return config;
    }
}
